package cn.mumzone.pos.presenter;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.event.JPushEvent;
import cn.mumzone.pos.model.ServerTimeTableEntity;
import cn.mumzone.pos.model.source.ReserveRepository;
import cn.mumzone.pos.presenter.contract.ServerTimeTableContract;
import cn.mumzone.pos.ui.fragment.FRA_ServerTimeTable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created By Dlx
 * on 2019/7/9
 * 是{@link FRA_ServerTimeTable}他的presenter
 */
public class ServerTimeTablePresenter extends AbsPresenter<ServerTimeTableContract.View, ReserveRepository> implements ServerTimeTableContract.Presenter {
    @Override
    public void getSeverTimeTable(String newDate, String orderType) {

        mView.showLoadingDialog();

        ApiCallBack<ServerTimeTableEntity> callBack = new ApiCallBack<ServerTimeTableEntity>() {
            @Override
            public void onSuccess(ServerTimeTableEntity entity) {
                if (entity.getTechnicianList() == null || entity.getTechnicianList().size() == 0) {
                    mView.showEmpty(entity.isScheduleOrTechnician());
                    mView.dismissLoadingDialog();
                } else {
                    mView.onServerTimeTableReceived(entity);
                    mView.hideNewsTip();
                    mView.dismissLoadingDialog();
                }
            }

            @Override
            public void onNetworkError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showNetError();
                super.onNetworkError(response);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
                mView.dismissLoadingDialog();
            }
        };
        mRepo.getServerTimeTable(newDate, orderType, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void addNewsTipsListener() {
        Subscription jpushReceiver = RxBus.getDefault()
                .toObservable(JPushEvent.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<JPushEvent>() {
                    @Override
                    public void call(JPushEvent jPushEvent) {
                        Elog.e("JPush", "已处理消息");
                        mView.showNews();
                    }
                });
        addSubscrebe(jpushReceiver);
    }
}
